import java.sql.*;
import java.util.Scanner;

public class JDBCDemoUpdate {

    public static final String URL = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT-8&useUnicode=true&characterEncoding=utf-8";

    public static final String UserName ="root";

    public static final String PassWord = "12345678";

    public static void TestConnection4(String username,String password,int id){
        try(Connection conn= DriverManager.getConnection(URL,UserName, PassWord)) {
            System.out.println("连接成功");

            //获得命令对象
//            PreparedStatement statement = conn.prepareStatement("insert into t_user(username,password)values(?,?)");
//            statement.setString(1, username);
//            statement.setString(2, password);


            PreparedStatement statement = conn.prepareStatement("update t_user set username=?,password=? where id=?");
            statement.setString(1,username);
            statement.setString(2,password);
            statement.setInt(3,id);

            //执行命令
            int rs = statement.executeUpdate();
            if (rs>0) {
                System.out.println("修改成功");
            }
            System.out.println("修改失败");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }







    public static void main(String[] args) {
        Scanner scanner4=new Scanner(System.in);
        System.out.println("请输入要修改的账号id：");
        int id4 = scanner4.nextInt();
        System.out.println("请输入修改后的账号：");
        String username4 = scanner4.next();
        System.out.println("请输入修改后的密码：");
        String password4 = scanner4.next();
        TestConnection4(username4,password4,id4);
    }
}
